#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define KEYLENGTH 15                   /* 关键词字符串的最大长度 */
typedef char ElementType[KEYLENGTH+1]; /* 关键词类型用字符串 */
typedef int Index;                     /* 散列地址类型 */
typedef enum {false, true} bool;

typedef struct LNode *PtrToLNode;
struct LNode {
    ElementType Data;
    PtrToLNode Next;
};
typedef PtrToLNode Position;
typedef PtrToLNode List;

typedef struct TblNode *HashTable; /* 散列表类型 */
struct TblNode {   /* 散列表结点定义 */
    int TableSize; /* 表的最大长度 */
    List Heads;    /* 指向链表头结点的数组 */
};

Index Hash(ElementType Key, int TableSize ) {
    return (Key[0]-'a')%TableSize;
}

HashTable BuildTable(); /* 裁判实现，细节不表 */
bool Delete( HashTable H, ElementType Key );

int main()
{
    HashTable H;
    ElementType Key;

    H = BuildTable();
    scanf("%s", Key);
    if (Delete(H, Key) == false)
        printf("ERROR: %s is not found\n", Key);
    if (Delete(H, Key) == true)
        printf("Are you kidding me?\n");
    return 0;
}

/* 你的代码将被嵌在这里 *
 *
 *
 * */

bool Delete(HashTable H, ElementType Key ){
    //先通过hash函数定位
    Index  index=Hash(Key,H->TableSize);
    List pre= H->Heads+index;

    while (pre->Next !=NULL && strcmp(pre -> Next -> Data,Key) !=0 ){
        pre=pre->Next;
    }
    //没找到
    if (pre->Next == NULL){
        return false;
    } else{
        PtrToLNode p=pre->Next;
        pre->Next=p->Next;
        printf("%s is deleted from list Heads[%d]",Key,index);
        free(p);
        return true;
    }

}


Position FindHash(HashTable H, ElementType Key ){
    Index  index=Hash(Key,H->TableSize);
    List pre= H->Heads+index;

    while (pre->Next !=NULL && strcmp(pre -> Next -> Data,Key) !=0 ){
        pre=pre->Next;
    }
    return pre->Next;
}

void Insert(ElementType key, HashTable H){
    List P;
    if (FindHash(H,key) == NULL) {
        int i = Hash(key, H->TableSize);
        List L = (List)malloc(sizeof(struct LNode));
        strcpy(L->Data, key);
        P = &H->Heads[i];
        L->Next = P->Next;  //头插法
        P->Next = L;
    }else{
        printf("Already exist!\n");
    }
}


HashTable InitializeTable(int length) {
    HashTable hashTable;
    int i;

    hashTable = (HashTable) malloc(sizeof(struct TblNode));
    hashTable->TableSize = length;


    hashTable->Heads = (List) malloc(sizeof(struct LNode) * length);
    for (i = 0; i < length; i++) {
        hashTable->Heads[i].Next = NULL;
    }

    return hashTable;
}
